package com.yanqu.road.server.logic.bag;

import com.yanqu.road.entity.bag.UseGoodsResult;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.patrons.PatronsInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2021/3/26 11:49
 */
public class UseAddPatronsBaiYeExp extends BaseUseGoods{

    public UseAddPatronsBaiYeExp(GamePlayer player, GoodsInfo goodsInfo, long param) {
        super(player, goodsInfo, param);
    }

    @Override
    int checkUse(long count) {

        int occupation = goodsInfo.getParamList().get(1).intValue();
        int combId = goodsInfo.getParamList().get(2).intValue();

        // 指定门客ID
        int patronsId = (int) param;

        if (occupation >= 0 && combId == 0) {
            // 1.随机某职业门客
            UserPatrons userPatrons = player.getModule(PatronsModule.class).getRandomPatrons(occupation);
            if(null == userPatrons){
                return GameErrorCode.E_BAG_ITEM_USE_NO_OCCUPATION_PATRONS;
            }
            if (patronsId != 0) {
                return GameErrorCode.E_BAG_ITEM_USE_NO_SET_PATRONS; // 随便拿个错误码
            }
        } else if (occupation == -1 && combId == 0) {
            // 2.选择门客
            if (0 == patronsId) {
                return GameErrorCode.E_BAG_ITEM_USE_NO_SET_PATRONS;
            }
            UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(patronsId);
            if (null == userPatrons) {
                return GameErrorCode.E_PATRONS_NO_FOUND;
            }
        } else if (occupation == -1 && combId > 0) {
            // 3.选择组合内的门客
            if (0 == patronsId) {
                return GameErrorCode.E_BAG_ITEM_USE_NO_SET_PATRONS;
            }
            UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(patronsId);
            if (null == userPatrons) {
                return GameErrorCode.E_PATRONS_NO_FOUND;
            }
            PatronsInfo patronsInfo = PatronsMgr.getPatronsInfo(userPatrons.getPatronsId());
            if (patronsInfo.getCombId() != combId) {
                return GameErrorCode.E_PATRONS_COMBID_ERROR;
            }
        } else {
            return GameErrorCode.E_BAG_ITEM_USE_TYPE_UNKNOWN; // 随便拿个错误码
        }

        return 0;
    }

    @Override
    UseGoodsResult executeUse(long count) {
        UseGoodsResult result = new UseGoodsResult();
        long isRandomExp = goodsInfo.getParamList().get(0).longValue();
        int occupation = goodsInfo.getParamList().get(1).intValue();

        PatronsModule patronsModule = player.getModule(PatronsModule.class);

        UserPatrons userPatrons = null;
        int patronsId = (int) param;
        if (patronsId != 0) {
            userPatrons = patronsModule.getUserPatrons(patronsId);
        }

        Property property = new Property();
        List<UserPatrons> syncList = new ArrayList<>();

        // 标记门客被加了多少次
        Map<Integer, Integer> patronsAddNumMap = new HashMap<>();

        //获取门客LIST
        List<UserPatrons> patronsList = patronsModule.getPatronsListBySkillConsumeId(occupation, GameConfig.PATRONS_BAIYE_EXP_ID);
        //遍历道具数量
        for(int i = 0; i < count; i++) {
            //道具增加的经验
            long addExp;
            if(0 == isRandomExp){
                //随机经验
                addExp = GoodsMgr.getAddRandomValue(goodsInfo).longValue();
            }else{
                //固定经验
                addExp = isRandomExp;
            }

            //增加经验的门客
            UserPatrons randomPatrons = userPatrons;
            if(null == randomPatrons) {
                //不是指定门客，随机出门客
                randomPatrons = patronsModule.getRandomPatronsByList(patronsList);
            }
            //数据整合
            if(null != randomPatrons) {
                //资产数据
                property.addProperty(randomPatrons.getPatronsId(), BigInteger.valueOf(addExp));
                //门客list
                if(!syncList.contains(randomPatrons)){
                    syncList.add(randomPatrons);
                }
                //门客使用道具次数
                patronsAddNumMap.put(randomPatrons.getPatronsId(), patronsAddNumMap.getOrDefault(randomPatrons.getPatronsId(), 0) + 1);
            }
        }

        // 统一日志及逻辑处理 防止因道具一次使用太多导致逻辑冗余处理及日志冗余
        for (Map.Entry<Integer, BigInteger> entry : property.getGoods().entrySet()) {
            int addPatronsId = entry.getKey();
            long addValue = entry.getValue().longValue();
            UserPatrons addUserPatrons = patronsModule.getUserPatrons(addPatronsId);
            //增加门客百业经验
            if (addUserPatrons != null) {
                long oldValue = addUserPatrons.getBaiYeExp();
                addUserPatrons.setBaiYeExp(addUserPatrons.getBaiYeExp() + addValue);
                LogMgr.addLogUseAddAttributeGoods(player.getUserId(), goodsInfo.getGoodsId(), goodsInfo.getType(), addPatronsId, addValue,
                        BigInteger.valueOf(oldValue), BigInteger.valueOf(addUserPatrons.getBaiYeExp()), patronsAddNumMap.getOrDefault(addPatronsId, 0));
            }

        }

        if(syncList.size() > 0){
            patronsModule.onPatronsListChange(syncList);
            patronsModule.syncPatronsData(syncList, false);
        }
        result.setReward(PropertyHelper.parsePropertyToString(property));
        return result;
    }
}
